class ListNode{
    int val;
    ListNode next;

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }

    public ListNode() {
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
public class Demo {
    public static boolean isRing(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        while (true){
            if(fast == null || fast.next == null) return false;
            slow = slow.next;
            fast = fast.next.next;
            if(fast == slow) break;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode();
        ListNode l2 = new ListNode();
        ListNode l3 = new ListNode();
        ListNode l4 = new ListNode();
        l1.setVal(3);
        l2.setVal(2);
        l3.setVal(0);
        l4.setVal(-4);
        l1.setNext(l2);
        l2.setNext(l3);
        l3.setNext(l4);
        l4.setNext(l2);
        boolean ring = isRing(l1);
        System.out.println(ring);
    }
}
